home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14158 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  56 lines

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning address of value to main
  5. Date: 12 Apr 1996 03:36:35 GMT
  6. Organization: systems hk
  7. Message-ID: <4kkj43$c32@nadine.teleport.com>
  8. References: <4khpjq$a3c@pipe1.nyc.pipeline.com>
  9. NNTP-Posting-Host: ip-pdx08-22.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. luciferm@nyc.pipeline.com (manjila thapa) wrote:
  16. >I want to return the address  of element of an array 
  17. >a[0]....a[n]....a[m-1]          (address of a[n] is to be returned)
  18. >from a function to the main so that:
  19. >
  20. >main(){
  21. >double *newarr;
  22. >...
  23. >...
  24. >newarr= function(......) /*function is supposed to return address*/
  25. >__________________________________________
  26. >i tried 
  27. >
  28. >return &a[n];
  29. >
  30. >in function but it doesn't work! Isn't this the right way to get the
  31. >address? (this is part of my hw, hope u don't mind) 
  32.  
  33. Manny,
  34. It should work if array 'a' is not local to the function, since the
  35. address you return from the function would point to an undefined
  36. location once the function returns.  'a' would have to be global or
  37. at least local to the calling routine and passed to the function, e.g.:
  38.  
  39. double  a[100];
  40. double  *newarr;
  41. double  *function ( double a[] );
  42.  
  43. ..
  44.   newarr = function( a );
  45. ..
  46.  
  47.  
  48. double *function ( double a[] )
  49. {
  50.    ...
  51.    return( &a[5] );
  52. }
  53.  
  54. Yours, Geoff
  55.  
  56.